package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.scity.beens.CwyyListBeen;
import com.example.scity.beens.HouseBeen;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CwyyActivity extends AppCompatActivity {
    ListView listView;
    CwyyListBeen cwyyListBeen;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    CwyyListView();
                    break;
            }
        }
    };

    private void CwyyListView() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return cwyyListBeen.rows.size();
            }

            @Override
            public Object getItem(int i) {
                return cwyyListBeen.rows.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                view = LayoutInflater.from(CwyyActivity.this).inflate(R.layout.item_cwyy, null);
                ImageView imageView;
                imageView = view.findViewById(R.id.item_cwyy_img);
                Glide.with(view).load(Configs.Host + cwyyListBeen.rows.get(i).avatar).into(imageView);
                TextView name;
                name = view.findViewById(R.id.item_cwyy_name);
                name.setText(cwyyListBeen.rows.get(i).name);
                TextView jobName;
                jobName = view.findViewById(R.id.item_cwyy_jobName);
                jobName.setText(cwyyListBeen.rows.get(i).jobName);
                TextView practiceNo;
                practiceNo = view.findViewById(R.id.item_cwyy_practiceNo);
                practiceNo.setText(cwyyListBeen.rows.get(i).practiceNo);
                TextView workingYears;
                workingYears = view.findViewById(R.id.item_cwyy_workingYears);
                workingYears.setText(cwyyListBeen.rows.get(i).workingYears);
                TextView goodAt;
                goodAt = view.findViewById(R.id.item_cwyy_goodAt);
                goodAt.setText(cwyyListBeen.rows.get(i).goodAt);
                return view;
            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cwyy);
        listView = findViewById(R.id.cwyy_listview);
        LoadCwyy();
    }
    private void LoadCwyy() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Configs.Host + APIInterface.CwyyList)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                cwyyListBeen = new Gson().fromJson(response.body().string(), CwyyListBeen.class);
                handler.sendEmptyMessage(0);
            }
        });
    }
}